/*
 * Copyright (c) 2025 Li Auto Inc. and its affiliates
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef SRC_CPP_RTPS_TRANSPORT_DSFLOCATOR_HPP_
#define SRC_CPP_RTPS_TRANSPORT_DSFLOCATOR_HPP_

#include <deps/common/Locator.h>
#include <deps/common/Host.hpp>

namespace vbsutil {
namespace xmlparser {

/**
 * Define operations for dsf locators
 */
class DSFLocator {
 public:
    // enum class Type { UNICAST, MULTICAST };

    /**
     * Generate a dsf locator for the local host.
     * @param pool_id
     * @param queue_id
     * @return The created dsf locator.
     */
    static Locator create_locator(uint32_t pool_id, uint32_t queue_id) {

        Locator_t locator(LOCATOR_KIND_DSF, pool_id);

        locator.get_address()[0] = octet(queue_id);
        locator.get_address()[1] = octet(queue_id >> 8);
        locator.get_address()[2] = octet(queue_id >> 16);
        locator.get_address()[3] = octet(queue_id >> 24);

        return locator;
    }

    /**
     * Generate a dsf locator for the local host.
     */
    static bool get_locator_information(Locator& locator, uint32_t& pool_id, uint32_t& queue_id) {
        queue_id = 0;
        pool_id = locator.port;

        uint8_t* address = locator.get_address();

        queue_id |= static_cast<uint32_t>(address[0]);
        queue_id |= static_cast<uint32_t>(address[1]) << 8;
        queue_id |= static_cast<uint32_t>(address[2]) << 16;
        queue_id |= static_cast<uint32_t>(address[3]) << 24;

        return true;
    }
};

}  // namespace xmlparser
}  // namespace vbsutil

#endif  // SRC_CPP_RTPS_TRANSPORT_DSFLOCATOR_HPP_
